package org.vectrics.skill.struts.pagelist;

import org.apache.log4j.Logger;
import org.vectrics.DataException;
import org.vectrics.SystemException;
import org.vectrics.common.util.DateSync;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.skill.SkillRatingSet;
import org.vectrics.skill.UserEvaluation;
import org.vectrics.skill.struts.PrepareWizardActionAbstract;
import org.vectrics.struts.Request;
import org.vectrics.user.Person;
import org.vectrics.user.PersonEncounterStatusType;
import org.vectrics.user.UserSkillRatingSet;

/**
 * @author mmoore
  */
public class PrepareSkillPageListAction extends PrepareWizardActionAbstract {
	private transient static Logger log = Logger.getLogger(PrepareSkillPageListAction.class);
	
	@Override
	protected boolean getNeedToAddImpliedSkills() {
		return true;
	}
	
	@Override
	protected SkillRatingSet getSkillRatingSet(Request request, Person authPerson) {
		SkillRatingSet set = null;
		try {
			//Person person = userMgr.findPersonById(new Long(this.getUserId()));
			String evaluationId = request.getParameter("userEvaluationId");
			if (evaluationId != null) {
				UserEvaluation evaluation = RecruitServiceLocator.getSkillProvider().findEvaluationById(new Long(evaluationId));
				set = RecruitServiceLocator.getSkillProvider().findUserSkillSet(evaluation);
				if (set == null)
					set = new UserSkillRatingSet(evaluation.getPerson());
			} else {
				UserEvaluation evaluation = new UserEvaluation();
				evaluation.setLeadPerson(authPerson);
				evaluation.setPerson(authPerson);
				evaluation.setDescription("User's Self Evaluation");
				evaluation.setDateStart(DateSync.getCurrentTime());
				evaluation.setDateCreated(DateSync.getCurrentTime());
				PersonEncounterStatusType encounterStatus = RecruitServiceLocator.getPersonService().findPersonEncounterStatusType(PersonEncounterStatusType.STATUS_SCHEDULED);
				evaluation.setEncounterStatus(encounterStatus);
				RecruitServiceLocator.getSkillProvider().updateEvaluation(evaluation);
				set = new UserSkillRatingSet(authPerson);
			}
		}
		catch (DataException de) {
			log.error("DataException finding user", de);
			throw new SystemException("DataException finding user", de);
		}
		return(set);
	}


}
